package com.example.casper.Experiment2024.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.NonNull;
import com.example.casper.Experiment2024.R;
import java.util.Calendar;
public class CustomClockView extends View {

    private Bitmap clockBitmap;
    private Bitmap hourBitmap;
    private Bitmap minuteBitmap;
    private Bitmap secondBitmap;

    private float hourAngle = 0;
    private float minuteAngle = 0;
    private float secondAngle = 0;

    private Paint paint;
    private Handler handler;

    public CustomClockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private Bitmap scaleBitmap(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale((float) 324 / bitmap.getWidth(), (float) 324 / bitmap.getHeight());
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    private void init() {
        clockBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.clockbground);
        hourBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hour);
        minuteBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.minute);
        secondBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.second);

        // 调整指针的大小
        hourBitmap = scaleBitmap(hourBitmap);
        minuteBitmap = scaleBitmap(minuteBitmap);
        secondBitmap = scaleBitmap(secondBitmap);

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);

        handler = new Handler(Looper.getMainLooper());
        updateTime();
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();
        float centerX = width / 2f;
        float centerY = height / 2f;
        canvas.drawBitmap(clockBitmap, null, new Rect(0, 0, width, height), paint);
        canvas.save();
        canvas.rotate(hourAngle, centerX, centerY);
        canvas.drawBitmap(hourBitmap, centerX - (float) hourBitmap.getWidth() / 1.8f, centerY - (float) hourBitmap.getHeight() / 2.16f, paint);
        canvas.restore();
        canvas.save();
        canvas.rotate(minuteAngle, centerX, centerY);
        canvas.drawBitmap(minuteBitmap, centerX - (float) minuteBitmap.getWidth() / 1.634f, centerY - (float) minuteBitmap.getHeight() / 2.73f, paint);
        canvas.restore();
        canvas.save();
        canvas.rotate(secondAngle, centerX, centerY);
        canvas.drawBitmap(secondBitmap, centerX - secondBitmap.getWidth() / 1.32f, centerY - (float) secondBitmap.getHeight() / 4.4f, paint);
        canvas.restore();
    }
    private void updateTime() {
        handler.postDelayed(() -> {
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            int second = calendar.get(Calendar.SECOND);
            hourAngle =(hour % 12) * 30 + ((float) minute / 2f)+136;
            minuteAngle = minute * 6+ 136;
            secondAngle = second * 6+136;
            invalidate();
            updateTime();
        }, 1000);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int size = Math.min(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
        setMeasuredDimension(size, size);
    }
}